*Define programs---------------------------------------------------------------
program drop _all
*Rank vars relative within shina
program define relrank
	*#1: variable to rank
	*#2: output variable
	sort shina_id `1'
	gen `2'=1 if shina_id!=shina_id[_n-1]
	local n=1
	while `n'<_N{
		local n0=`n'-1
		if shina_id[`n']==shina_id[`n0']{
			if `1'[`n']==`1'[`n0']{
				replace `2'=`2'[`n0'] if _n==`n'
				}
			if `1'[`n']!=`1'[`n0']{
				replace `2'=`2'[`n0']+1 if _n==`n'
				}
		}
		local n=`n'+1
		}
	replace `2'=. if missing(`1')
end
*Rank vars relative within shina-leader
program define relrank_ldr
	*#1: variable to rank
	*#2: output variable
	sort shina_id leader_id `1'
	gen `2'=1 if shina_id!=shina_id[_n-1] | leader_id!=leader_id[_n-1]
	local n=1
	while `n'<_N{
		local n0=`n'-1
		if shina_id[`n']==shina_id[`n0'] & leader_id[`n']==leader_id[`n0']{
			if `1'[`n']==`1'[`n0']{
				replace `2'=`2'[`n0'] if _n==`n'
				}
			if `1'[`n']!=`1'[`n0']{
				replace `2'=`2'[`n0']+1 if _n==`n'
				}
		}
		local n=`n'+1
		}
	replace `2'=. if missing(`1')
end

*Parameters--------------------------------------------------------------------
local exch=2288 //Exchange rate TSH per USD

*=================================================================*
*--------------------Clean up data for regressions----------------*
*=================================================================*
use "${sdir}/Leaders_and_Owners_cln.dta", clear


*Create ranks------------------------------------------------------------------

*within-shina ranking outcomes
relrank s9_q_1_own rank_wtp 		//willingness-to-pay

*flip leader rankings and create within-shina ranks (so that unobserved respondents not included in ranks)
gen tmp_wtp=16-q41_ldr 	if owner_observed==1

relrank_ldr tmp_wtp 	rank_wtp_ldr
*drop tmp_*


*Top and bottom ranks
bysort shina_id: egen maxrelrank=max(rank_wtp)
gen toprank_wtp=(maxrelrank==rank_wtp) if owner_observed==1
gen botrank_wtp=(rank_wtp==1) if owner_observed==1
bysort shina_leader_id: egen maxrelrank_ldr=max(rank_wtp_ldr)
gen toprank_wtp_ldr=(maxrelrank_ldr==rank_wtp_ldr) if owner_observed==1
gen botrank_wtp_ldr=(rank_wtp_ldr==1) if owner_observed==1

*top and bottom of full distribution
bysort shina_plot_id: gen check=_n
sum s9_q_1_own if check==1, d
gen wtp90=(s9_q_1_own>`r(p90)') if missing(s9_q_1_own)==0
gen wtp10=(s9_q_1_own<`r(p10)') if missing(s9_q_1_own)==0
gen wtp90_ldr=(q42_ldr>`r(p90)') if missing(s9_q_1_own)==0
gen wtp10_ldr=(q42_ldr<`r(p10)') if missing(s9_q_1_own)==0
drop check

*Physical characteristics of property------------------------------------------
*valuation
gen value=value_pltpht_own/`exch'
gen ln_value =ln(value_pltpht_own)

relrank value rank_value
xtile pctl_value		=value, nq(100)
bysort shina_id: egen maxrelvalue=max(rank_value)
gen toprank_value=(maxrelvalue==rank_value) if owner_observed==1
gen botrank_value=(rank_value==1) if owner_observed==1
*invoice
gen invoice=invoice_value_own/`exch'
gen ln_invoice =ln(invoice)
relrank invoice rank_invoice
xtile pctl_invoice		=invoice, nq(100)
bysort shina_id: egen maxrelinvoice=max(rank_invoice)
gen toprank_invoice=(maxrelinvoice==rank_invoice) if owner_observed==1
gen botrank_invoice=(rank_invoice==1) if owner_observed==1

*make within plot id
bysort shina_id plot_id: gen within_plot_id=_n

*Rename and relabel------------------------------------------------------------
rename (s9_q_1_own q42_ldr)(wtp wtp_ldr)
replace wtp=wtp/`exch'
replace wtp_ldr=wtp_ldr/`exch'

label var rank_wtp 		"\makecell{WTP\\Rank}"
label var wtp 			"WTP"
label var toprank_wtp 	"\makecell{Top\\Rank}"
label var botrank_wtp 	"\makecell{Bottom\\Rank}"
label var wtp90 		"\makecell{Top\\10\%}"
label var wtp10 		"\makecell{Bottom\\10\%}"

save "temp/run_predictions_wtp.dta", replace

*=================================================================*
*------------------ Leader Prediction Regressions-----------------*
*=================================================================*
local reg_lst wtp rank_wtp toprank_wtp botrank_wtp wtp90 wtp10
local controls_lst gender_ldr q9_1_ldr q4_1_ldr surveyor_id_ldr 

local c=0
foreach v in `reg_lst'{
    local c=`c'+1
	preserve
	
	gen temp=`v'_ldr
	
	*get mean outcome
	reg `v' if within_plot_id==1 & missing(temp)==0
	local meanC=_b[_cons]
	local meanCdi: di %3.2f `meanC'
	
	*Panel A: Predictions
	eststo pnl1_`c': reghdfe `v' temp, vce(cluster shina_leader_id) absorb(strata trt_grp_ldr)
	estadd local meanC="`meanCdi'"
	local wR2di: di %5.4f e(r2_a_within)
	estadd local wR2="`wR2di'"
	
	*Panel B: within shina
	eststo pnl2_`c': reghdfe `v' temp, vce(cluster shina_leader_id) absorb(strata trt_grp_ldr shina_id)
	local wR2di: di %5.4f e(r2_a_within)
	estadd local wR2="`wR2di'"
	
	*Panel C: controls
	eststo pnl3_`c': reghdfe `v' temp, vce(cluster shina_leader_id) absorb(strata trt_grp_ldr `controls_lst')
	local wR2di: di %5.4f e(r2_a_within)
	estadd local wR2="`wR2di'"
	
	*Panel D: within shina + controls
	eststo pnl4_`c': reghdfe `v' temp, vce(cluster shina_leader_id) absorb(strata trt_grp_ldr shina_id `controls_lst')
	local wR2di: di %5.4f e(r2_a_within)
	estadd local wR2="`wR2di'"
	restore
	}
	
gen temp=.
label var temp "Leader Prediction"
	
*write to file
local note ///
	This table presents coefficients from regressions of willingness-to-pay on leader's predicitons of willingness-to-pay. Each observation is a leader-plot owner pair. Each column is a different measure of willingness-to-pay: col. 1 in USD, col. 2 is the within neighbourhood rank, col. 3 is an indicator for the highest in the neighbourhood, col. 4 is an indicator for the lowest, col.5 is an indicator if in the top 10\% of the full distribution, and col. 5 if in the bottom 10\% of the full distribution. Leader treatment group and randomization strata fixed effects are always included as controls. Each panel represents a different set of controls: panel A has no additional controls, panel B adds shina (neighbourhood) fixed effects, and panel C further adds a set of controls (leader gender, leader household income bracket, a dummy for leader's home plot being surveyed, and fixed effects for the ID of the leader's surveyor). Standard errors in parentheses are clustered at leader level. \sym{*}\(p<0.10\),	\sym{**}\(p<0.05\), \sym{***}\(p<0.01\).
		
*put all panels together in one table
//top panel 
esttab pnl1* using "tables/predictions_wtp.tex", ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(meanC N r2 wR2, label("Mean outcome" "Observations" "R$^2$" "within-R$^2$")) ///
	se label wrap fragment ///
	nobaselevels nocons ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} {\centering\begin{tabular}{l*{6}{c}} \hline\hline") ///
	posthead("\hline \\ \multicolumn{5}{l}{\textit{Panel A: Baseline controls}} \\\\[-1ex]") ///
	replace 

//middle panel #1
esttab pnl2* using "tables/predictions_wtp.tex", ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N r2 wR2, label("Observations" "R$^2$" "within-R$^2$")) ///
	se label wrap fragment ///
	nomtitles nonumbers ///
	nobaselevels nocons ///
	posthead("\hline \\ \multicolumn{5}{l}{\textit{Panel B: + controls for neighbourhood FE}} \\\\[-1ex]") ///
	append
//bottom panel 
esttab pnl4* using "tables/predictions_wtp.tex", ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N r2 wR2, label("Observations" "R$^2$" "within-R$^2$")) ///
	se label wrap fragment ///
	nomtitles nonumbers ///
	nobaselevels nocons ///
	posthead("\hline \\ \multicolumn{5}{l}{\textit{Panel C: + leader characteristic controls}} \\\\[-1ex]") ///
	prefoot("\hline") ///
	postfoot("\hline\hline \end{tabular}\par\medskip}{\footnotesize {\it Note: }`note'}") ///
	append
estimates clear

*------------------------------------------------------------------------------
clear
*END-------
